package chpt12;

import java.sql.*;
import javax.sql.rowset.*;

public class DBbasic3Cache {
	public static void main(String[] args) {
		String url = "jdbc:mysql://127.0.0.1:3306/demoDB?serverTimezone=Asia/Chongqing";
		String userName = "root";
		String pwd = "root123";
		String sql = "select * from students";
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, userName, pwd);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			RowSetFactory factory = RowSetProvider.newFactory();
			CachedRowSet cache=factory.createCachedRowSet(); // CachedRowSetImpl crs = new CachedRowSetImpl();
//			cache.populate(rs); // 用rs来填充cache，下次直接使用cache不要重新查询以提高效率
			cache.setMaxRows(4);
			cache.setPageSize(3);
			cache.populate(rs,2); 
			
			con.close();  // 关闭连接（当Connection关闭，rs也将关闭而无法使用）
			while (cache.next()) {
				System.out.println("学号=" + cache.getString(2) + 
						"; 姓名=" + cache.getString(3) + 
						"; GPA=" + cache.getDouble(4));
			}
		} catch (SQLException e){
			e.printStackTrace();
		}
	}
}
